<?php

class CategoriesController extends AppController {
    var $name = 'Categories'; // riferimento alla cartella delle view
	
    public function beforeFilter(){
    	
		parent::beforeFilter();
		
    }

	public function beforeRender(){
		// if(isset($this->viewVars['category'])) {
				// // per la gestione del logo
				// $category = $this->viewVars['category'];
				// // per la gestione della descrizione multilingua
				// if(isset($this->viewVars['category']['CategoryLang'])){
					// foreach ($this->viewVars['category']['CategoryLang'] as $key => $name) {
						// $this->viewVars['category']['CategoryLang'][$name['locale']]=$name;
						// unset($this->viewVars['category']['CategoryLang'][$key]);
					// }
				// }
		// }
	}
	
    public function index() {
    	
    	$this->Category->hasMany['CategoryLang']['conditions'] = array('CategoryLang.locale' => Configure::read("Config.language"));
        // $data = $this->Category->find('threaded');
        $data = $this->Category->generateTreeList(null, null, null, '&nbsp;&nbsp;&nbsp;&nbsp;');
		$this->set("categories",$data);
    }
	
	public function add() {

    	// $this->Category->bindTranslation(array('name' => 'nameTranslation'));
		// ricerco tutte le possibili categorie padre da poter assegnare
        $data = $this->Category->generateTreeList(null, null, null, '&nbsp;&nbsp;&nbsp;');
		// print_r($data);die;
		$this->set("categories",$data);
	}

    public function edit($id) {

    	// $this->Category->bindTranslation(array('name' => 'nameTranslation'));
		$this->Category->id = $id;
		$this->set('category', $this->Category->read());
		
		// ricerco tutte le possibili categorie padre da poter assegnare
        $data = $this->Category->generateTreeList(array('Category.id !='=>$id), null, null, '&nbsp;&nbsp;&nbsp;');
		// print_r($data);die;
		$this->set("categories",$data);

    }
	
	public function save_i18n() {
    	// $ds = $this->Category->getDataSource();
		// $ds->begin();
		// try {
			// $this->Category->setLanguage("ita");
			// $this->Category->set($this->request->data["Category"]);
			// $this->Category->set("name",$this->request->data["it_descrizione"]);
			// $this->Category->setUtente($this->Auth->user()); 
			// $this->Category->save();
// 
			// $this->Category->setLanguage("eng");
			// $this->Category->set("name",$this->request->data["en_descrizione"]);
			// $this->Category->setUtente($this->Auth->user()); 
			// $this->Category->save();
// 
			// $ds->commit();
			// $this->Session->setFlash(__("Categoria aggiornata."));
			// $this->redirect("/Categories/edit/".$this->Category->id);
		// } catch (exception $e) {
			// if(isset($ds)) {
				// $ds->rollback();
			// }
			// CakeLog::error($e->getMessage());
			// $this->Session->setFlash($e->getMessage(),null,null,LOG_ERR);
			// $this->redirect($this->referer());
		// }
	}
	
	public function save() {
		    $ds = $this->Category->getDataSource();
			$ds->begin();
		    try {
				$this->Category->setUtente($this->Auth->user());
			    $this->Category->save($this->request->data);
			    // Controller::loadModel("CategoryLang");
			    // $this->CategoryLang->set($this->request->data["ita"]);
			    // if (!isset($this->request->data["ita"]["id"]) || empty($this->request->data["ita"]["id"])) {
				    // $this->CategoryLang->set($this->request->data["ita"]);
					// $this->CategoryLang->set("help_id",$this->Help->id);
			    // }
				// $this->CategoryLang->set("testo",htmlentities($this->request->data["ita"]["CategoryLang"]["testo_non_codificato"], ENT_QUOTES));
			    // $this->CategoryLang->save();
			    // if (!isset($this->request->data["eng"]["id"]) || empty($this->request->data["eng"]["id"])) {
			    	// $this->CategoryLang->create();
				    // $this->CategoryLang->set($this->request->data["eng"]);
					// $this->CategoryLang->set("help_id",$this->Help->id);
			    // } else {
				    // $this->CategoryLang->set($this->request->data["eng"]);
			    // }
				// $this->CategoryLang->set("testo",htmlentities($this->request->data["eng"]["CategoryLang"]["testo_non_codificato"], ENT_QUOTES));
			    // $this->CategoryLang->save();
				$ds->commit();
				$this->Session->setFlash(__("Categoria Aggiornata."));
		    } catch (exception $e) {
		    	if (isset($ds)) {
		    		$ds->rollback();
		    	}
		    	CakeLog::error($e->getMessage());
				$this->Session->setFlash(__("Categoria non aggiornata:"). $e->getMessage(),null,null,LOG_ERR);
		    }
		if (isset($this->Category->id)) {
			$this->redirect("/Categories/edit/".$this->Category->id);
		} else {
			$this->redirect($this->referer());
		}
	}
	
	
	
    public function delete($id='') {
		if ($id=='')
			$id = $this->request->data['id'];
		$this->Category->delete($id);
		$this->Session->setFlash(__("Categoria eliminata."));
		$this->redirect("/Categories");

    }
	
}

?>